#ifndef _zgn_H
#define _zgn_H

//4x4键盘驱动     在setup中调用jianpan_init()后   在循环中调用get_jian()，此函数返回按下的键值  第一行第一个为1    的一行第二个为2 以此类推


#define R1 14//第一行
#define R2 27
#define R3 26
#define R4 25//第四行
#define c1 15//第一列
#define c2 4
#define c3 17
#define c4 18//第四列


void jianpan_init(){
     pinMode(R1, OUTPUT);
     pinMode(R2, OUTPUT);
     pinMode(R3, OUTPUT);
     pinMode(R4, OUTPUT);

     pinMode(c1, INPUT);
     pinMode(c2, INPUT);
     pinMode(c3, INPUT);
     pinMode(c4, INPUT);
 
}

int an_jian_zhi()
{
   int jianzhi = 0;

   digitalWrite(R1,HIGH);
   digitalWrite(R2,LOW);
   digitalWrite(R3,LOW);
   digitalWrite(R4,LOW);
   if(digitalRead(c1)==1){delay(15);if(digitalRead(c1)==1)jianzhi=1;}
   if(digitalRead(c2)==1){delay(15);if(digitalRead(c2)==1)jianzhi=2;}
   if(digitalRead(c3)==1){delay(15);if(digitalRead(c3)==1)jianzhi=3;}
   if(digitalRead(c4)==1){delay(15);if(digitalRead(c4)==1)jianzhi=4;}
   if(jianzhi>0){goto go;} 
   
   

   digitalWrite(R1,LOW);
   digitalWrite(R2,HIGH);
   digitalWrite(R3,LOW);
   digitalWrite(R4,LOW);
   if(digitalRead(c1)==1){delay(15);if(digitalRead(c1)==1)jianzhi=5;}
   if(digitalRead(c2)==1){delay(15);if(digitalRead(c2)==1)jianzhi=6;}
   if(digitalRead(c3)==1){delay(15);if(digitalRead(c3)==1)jianzhi=7;}
   if(digitalRead(c4)==1){delay(15);if(digitalRead(c4)==1)jianzhi=8;}
   if(jianzhi>0){goto go;} 


   digitalWrite(R1,LOW);
   digitalWrite(R2,LOW);
   digitalWrite(R3,HIGH);
   digitalWrite(R4,LOW);
   if(digitalRead(c1)==1){delay(15);if(digitalRead(c1)==1)jianzhi=9;}
   if(digitalRead(c2)==1){delay(15);if(digitalRead(c2)==1)jianzhi=10;}
   if(digitalRead(c3)==1){delay(15);if(digitalRead(c3)==1)jianzhi=11;}
   if(digitalRead(c4)==1){delay(15);if(digitalRead(c4)==1)jianzhi=12;}
   if(jianzhi>0){goto go;} 

   
   digitalWrite(R1,LOW);
   digitalWrite(R2,LOW);
   digitalWrite(R3,LOW);
   digitalWrite(R4,HIGH);
   if(digitalRead(c1)==1){delay(15);if(digitalRead(c1)==1)jianzhi=13;}
   if(digitalRead(c2)==1){delay(15);if(digitalRead(c2)==1)jianzhi=14;}
   if(digitalRead(c3)==1){delay(15);if(digitalRead(c3)==1)jianzhi=15;}
   if(digitalRead(c4)==1){delay(15);if(digitalRead(c4)==1)jianzhi=16;}
   if(jianzhi>0){goto go;} 


   go:return jianzhi;
   
}


int get_jian(){
  int jian = an_jian_zhi();
  delay(10);
  if(jian == an_jian_zhi()) {return jian;}else return 0;

}



#endif //

